﻿using System;
using System.Data;
using TopBuilds.Database;

namespace TopBuilds.Restaurant.Data
{
    public class SalePromotionObject
    {
        public static SalePromotion[] GetSalePromotions()
        {
            return Query(null);
        }

        public static SalePromotion GetSalePromotion(int id)
        {
            SalePromotion salePromotion = new SalePromotion();

            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(SalePromotionDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = SalePromotionDesc.TableName;
            string sql = builder.QueryCommandLine;

            DataTable salePromotions = DatabaseObject.Instance.ExecuteQuerySQL(sql);

            salePromotion.Id                           = Convert.ToInt32(salePromotions.Rows[0][SalePromotionDesc.Id]);
            salePromotion.Name                         = Convert.ToString(salePromotions.Rows[0][SalePromotionDesc.Name]);
            salePromotion.StartTime                    = Convert.ToDateTime(salePromotions.Rows[0][SalePromotionDesc.StartTime]);
            salePromotion.EndTime                      = Convert.ToDateTime(salePromotions.Rows[0][SalePromotionDesc.EndTime]);
            salePromotion.Discount                     = Convert.ToInt32(salePromotions.Rows[0][SalePromotionDesc.Discount]);

            return salePromotion;
        }

        public static SalePromotion GetSalePromotion(string name)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(SalePromotionDesc.Name, ConditionExpress.EQUALS, name);

            SalePromotion[] set = Query(builder.ConditionString);

            if (set != null && set.Length > 0)
            {
                return set[0];
            }

            return null;
        }

        public static SalePromotion[] Query(string condition)
        {

            // query factories from database
            string sql = "select * from " + SalePromotionDesc.TableName;
            if (condition != null && condition.Length > 0)
            {
                sql += " where " + condition;
            }
            DataTable table = DatabaseObject.Instance.ExecuteQuerySQL(sql);


            // rebuild the data
            if (table != null && table.Rows.Count > 0)
            {
                SalePromotion[] salePromotions = new SalePromotion[table.Rows.Count];

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    SalePromotion salePromotion = new SalePromotion();

                    salePromotion.Id                  = Convert.ToInt32(table.Rows[i][SalePromotionDesc.Id]);
                    salePromotion.Name                = Convert.ToString(table.Rows[i][SalePromotionDesc.Name]);
                    salePromotion.StartTime           = Convert.ToDateTime(table.Rows[i][SalePromotionDesc.StartTime]);
                    salePromotion.EndTime             = Convert.ToDateTime(table.Rows[i][SalePromotionDesc.EndTime]);
                    salePromotion.Discount            = Convert.ToInt32(table.Rows[i][SalePromotionDesc.Discount]);

                    salePromotions.SetValue(salePromotion, i);
                }

                return salePromotions;
            }
            return null;
        }

        public static Int32 Add(SalePromotion salePromotion)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.InsertAdd(SalePromotionDesc.Name,                salePromotion.Name);
            builder.InsertAdd(SalePromotionDesc.StartTime,           salePromotion.StartTime);
            builder.InsertAdd(SalePromotionDesc.EndTime,             salePromotion.StartTime);
            builder.InsertAdd(SalePromotionDesc.Discount,            salePromotion.Discount);
            builder.QueryTableName = SalePromotionDesc.TableName;

            string sql = builder.InsertCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }

        public static bool Delete(Int32 id)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(SalePromotionDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = SalePromotionDesc.TableName;
            string sql = builder.DeleteCommandLine;

            return (DatabaseObject.Instance.ExecuteNonQuerySQL(sql) != -1);
        }

        public static Int32 Update(SalePromotion salePromotion)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.UpdateAdd(SalePromotionDesc.Name,                   salePromotion.Name);
            builder.UpdateAdd(SalePromotionDesc.StartTime,              salePromotion.StartTime);
            builder.UpdateAdd(SalePromotionDesc.EndTime,                salePromotion.StartTime);
            builder.UpdateAdd(SalePromotionDesc.Discount,               salePromotion.Discount);
            builder.ConditionAdd(SalePromotionDesc.Id, ConditionExpress.EQUALS, salePromotion.Id);
            builder.QueryTableName = SalePromotionDesc.TableName;

            string sql = builder.UpdateCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }
    }
}
